package com.example.administrator.meishi.fragment;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.example.administrator.meishi.Constract;
import com.example.administrator.meishi.Dao.GridViewJson;
import com.example.administrator.meishi.R;
import com.example.administrator.meishi.adapter.PicAdapter;
import com.example.administrator.meishi.bean.MediaTitle;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;

import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class ContentFragment extends Fragment {

    private int page;
    private String path;
    private List<MediaTitle> ab;
    private int id;
    private PullToRefreshGridView grid_view;

    public ContentFragment() {
        // Required empty public constructor
    }

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == 2) {
                ab = (List<MediaTitle>) msg.obj;
                PicAdapter adapter = new PicAdapter(ab, getActivity());
                grid_view.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                grid_view.onRefreshComplete();
            } else if (msg.what == 3) {
                List<MediaTitle> cd = (List<MediaTitle>) msg.obj;
                ab.addAll(cd);
                PicAdapter adapter = new PicAdapter(ab, getActivity());
                grid_view.setAdapter(adapter);
                //adapter.notifyDataSetChanged();
                grid_view.onRefreshComplete();
            }
            return false;
        }
    });

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_content, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        page = 1;
        //获取传输过来的id，并拼接成新的地址
        Bundle bundle = getArguments();
        id = bundle.getInt("num");
        path = Constract.BASE_URL + Constract.VIDEO_LIST + "?id=" + id + "&&page" + page + "&&count15";
        //获取gridview
        grid_view = (PullToRefreshGridView) view.findViewById(R.id.grid_view);
        initData();
        initListener();
    }

    private void initListener() {
        //gridview的滑动监听
        grid_view.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        initData();
                    }
                }, 2000);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        initDatatwo();
                    }
                }, 2000);

            }
        });
    }

    private void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                //解析json数据，并将得到的json数据利用handler进行传输
                List<MediaTitle> getjson = GridViewJson.getjson(Constract.BASE_URL + Constract.VIDEO_LIST + "?id=" + id + "&&page=" + 1 + "&&count=15");
                Message message = Message.obtain();
                message.what = 2;
                message.obj = getjson;
                handler.sendMessage(message);
            }
        }).start();
    }

    private void initDatatwo() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                page++;
                //解析json数据，并将得到的json数据利用handler进行传输
                List<MediaTitle> getjson = GridViewJson.getjson(Constract.BASE_URL + Constract.VIDEO_LIST + "?id=" + id + "&&page=" + page + "&&count=15");
                Message message = Message.obtain();
                message.what = 3;
                message.obj = getjson;
                handler.sendMessage(message);
            }
        }).start();
    }

}
